using System.Collections.Generic;

namespace ShadeTree.Binding.Elements
{
    public class RadioButtonGroup<ENUM>
    {
        private readonly List<RadioElement<ENUM>> _list = new List<RadioElement<ENUM>>();
        private bool _latched;

        public void Add(RadioElement<ENUM> button)
        {
            _list.Add(button);
        }


        public void ApplyChanges(object target)
        {
            if (_latched)
            {
                return;
            }

            _latched = true;

            foreach (var element in _list)
            {
                element.ApplyChanges();
            }

            _latched = false;
        }


        public void LatchPeers(RadioElement<ENUM> element)
        {
            foreach (var radioElement in _list)
            {
                if (!ReferenceEquals(radioElement, element))
                {
                    radioElement.Latched = true;
                }
            }
        }

        public void UnLatchPeers(RadioElement<ENUM> element)
        {
            foreach (var radioElement in _list)
            {
                if (!ReferenceEquals(radioElement, element))
                {
                    radioElement.Latched = false;
                }
            }
        }
    }
}